package de.koller.aframe.gl.obj;

import de.koller.aframe.gl.AVertexBuffer;
import de.koller.aframe.gl.AVertexBuffer.UsageType;
import org.lwjgl.opengl.GL11;

public class Cube {
	
	private final AVertexBuffer.UsageType usage;
	private AVertexBuffer vbo = null;

	public Cube( UsageType usage ) {
		this.usage = usage;
	}
	
	public static Cube create() {
		return new Cube( AVertexBuffer.UsageType.Static );
	}
	
	public static Cube create( boolean dynamic ) {
		return new Cube( dynamic ? AVertexBuffer.UsageType.Dynamic : AVertexBuffer.UsageType.Static );
	}
	
	private void initCube() {
		if( vbo != null )
			return;
		
		vbo = AVertexBuffer.create( usage, GL11.GL_QUADS );
		vbo.setup( 3, 0, 0, 2 );
		
		vbo.setData(
				-.5f,  .5f,  .5f,   0, 0,
				-.5f, -.5f,  .5f,   0, 1,
				 .5f, -.5f,  .5f,   1, 1,
				 .5f,  .5f,  .5f,   1, 0,
		
				 .5f,  .5f,  .5f,   0, 0,
				 .5f, -.5f,  .5f,   0, 1,
				 .5f, -.5f, -.5f,   1, 1,
				 .5f,  .5f, -.5f,   1, 0,
		
				 .5f,  .5f, -.5f,   0, 0,
				 .5f, -.5f, -.5f,   0, 1,
				-.5f, -.5f, -.5f,   1, 1,
				-.5f,  .5f, -.5f,   1, 0,
		
				-.5f,  .5f, -.5f,   0, 0,
				-.5f, -.5f, -.5f,   0, 1,
				-.5f, -.5f,  .5f,   1, 1,
				-.5f,  .5f,  .5f,   1, 0,
		
				-.5f,  .5f, -.5f,   0, 0,
				-.5f,  .5f,  .5f,   0, 1,
				 .5f,  .5f,  .5f,   1, 1,
				 .5f,  .5f, -.5f,   1, 0,
		
				-.5f, -.5f,  .5f,   0, 0,
				-.5f, -.5f, -.5f,   0, 1,
				 .5f, -.5f, -.5f,   1, 1,
				 .5f, -.5f,  .5f,   1, 0
		);
	}
	
	public void renderThis() {
		initCube();
		vbo.renderThis();
		
	}
	
	public void renderThis( float x, float y, float z ) {
		GL11.glPushMatrix();
		
		GL11.glTranslatef( x, y, z );
		renderThis();
		
		GL11.glPopMatrix();
	}
	
	public void renderThis( float x, float y, float z, float w, float h, float d ) {
		GL11.glPushMatrix();
		
		GL11.glTranslatef( x, y, z );
		GL11.glScalef( w, h, d );
		renderThis();
		
		GL11.glPopMatrix();
	}
	
}
